#!/usr/bin/perl

use strict;

use DBI;
use CGI;
use TTraker;

my $dbh;
& initialize_dbi;
my $form_ref = &parse_input;
& print_header;

my %FORM_DATA = %$form_ref;

my $num = $FORM_DATA{'supNum'};
my $name = $FORM_DATA{'supName'};
my $job = $FORM_DATA{'supJob'};
my $loc = $FORM_DATA{'supLoc'};
my $date = $FORM_DATA{'payDate'};
my $msg = "";

# update_daily_time_stats ($loc, $date);

confirm_payroll_hours ( $num, $name, $job, $loc, $date);

db_disconnect();

############### SUBROUTINES FOLLOW ###############


sub confirm_payroll_hours
{
	my $num =  $_[0];
	my $name =  $_[1];
	my $job =   $_[2];
	my $loc = $_[3];
	my $date = $_[4];
	my $msg;
	# print "2 $loc, $date<br/>";
	my $sw = 0;
	my $sw = check_for_weekend ($date);
	if ( $sw == 1 ) {
	   add_active_employees_to_payroll ($loc, $date);
	}
	update_start_end_elapsed_for_payroll_hrs ($loc, $date);
	my $pay_count = get_pay_count ($loc, $date) ;
   if ( $pay_count == 0 ) {
      if ( $sw == 0) {
	      $msg = "Weekend - no open payroll events for $date! Nothing to confirm.";
      } else {
	      $msg = "No open payroll events for $date! Nothing to confirm.";
     	}
     	# print "THE MESSAGE: $msg<br/>";
		sup_activity_screen_from_pm ($num, $name, $job, $loc, $msg) ;
   } else {
		trak_03_sup_confirm_pay_hours ($loc, $date, $num, $name);
   }
}

sub trak_03_sup_confirm_pay_hours
{
  my $loc = $_[0];
  my $date = $_[1];
  my $num =  $_[2];
  my $name =  $_[3];
   my @pay=();
  my $pay_ref = get_payroll_events ($loc, $date);
  @pay = @$pay_ref;
#  #CC9999, #9999CC
  print <<ENDHEADER;
  <html>
  <head>
  	<style type="text/css">
	tr.d0 td {
		background-color: #315B7E; color: white;
	}
	tr.d1 td {
		background-color: #4682B4; color: white;
	}
	</style>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_03_sup_confirm_payroll.pl</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  <div id='box5_1'>
  <h3>Confirm Payroll Hours $date</h3>
  <form onkeypress='return event.keyCode != 13;' autocomplete='off'  name='activity' action='${cgi_ref}trak_04_sup_update_payroll.pl' method='post'>
  <input type="hidden" name="supNum" value='$num'/>
  <input type="hidden" name="supName" value='$name'/>
  <input type="hidden" name="supJob" value='$job'/>
  <input type="hidden" name="supLoc" value='$loc'/>
  <input type="hidden" name="startDate" value='$date'/>
  <table table id="medium">
    <tr>
      <th>Emp#</th>
      <th>Name</th>
      <th>Start</th>
      <th>End</th>
      <th>Lun</th>
      <th>Net</th>
      <th>Stat</th>
      <th>Util</th>
      <th>Total</th>
      <th>&nbsp&nbsp</th>
      <th>Reg</th>
      <th>1.5x</th>
      <th>2.0x</th>
      <th>Vac</th>
      <th>Sta</th>
      <th>Sck</th>
      <th>Brv</th>
      <th>Abs</th>
      <th>Loa</th>
    </tr>
ENDHEADER
 my $tab = 1;
 my $r=0;
  for my $i (0..$#pay ) {
 	my $id = $pay[$i][0];
   my $name = $pay[$i][1];
   my $num = $pay[$i][2];
	my $elap = $pay[$i][3];
	my $lunch = sprintf "%d", $pay[$i][4];
    my $oriNet = int($pay[$i][5] * 4) / 4;
   my $net = sprintf "%.2f", $oriNet;

   my $start = $pay[$i][6];
   my $end = $pay[$i][7];
   my $comm = $pay[$i][8];
	my $Util = $pay[$i][9];
	my $util = sprintf "%2d \%", $Util;
	my $hours = $pay[$i][10];
	my $reg = $pay[$i][11];
	my $ot1 = $pay[$i][12];
	my $ot2 = $pay[$i][13];
	my $vac = $pay[$i][14];
	my $sta = $pay[$i][15];
	my $sic = $pay[$i][16];
	my $brv = $pay[$i][17];
	my $abs = $pay[$i][18];
	my $loa = $pay[$i][19];
	if (not defined($net)) {
		$net = $elap
	}
	my $sty;
 	if ($r%2 == 0) {
	 	$sty = "d0";
 	} else {
 		$sty = "d1";
 	}
   my $status = get_status ($num, $date);
	my $Start = substr $start, 11, 5;
	my $End =  substr $end, 11, 5;
	my $Clock = substr $net, 0, 5;
	my $t1=$tab;
 	my $t2=$tab+1+$#pay;
 	my $t3=$tab+2+$#pay*2;
 	my $t4=$tab+3+$#pay*3;
 	my $t5=$tab+4+$#pay*4;
 	my $t6=$tab+5+$#pay*5;
 	my $t7=$tab+6+$#pay*6;
 	my $t8=$tab+7+$#pay*7;
 	my $t9=$tab+8+$#pay*8;
   print <<ENDHEADER;
   <tr class = "$sty">
     <td>&nbsp&nbsp&nbsp $num</td>
     <td><a href="trak_04_sup_daily_summary.pl?num=$num&name=$name&date=$date&total=$hours&fancy_date=$date" target="_blank">&nbsp&nbsp&nbsp $name</td>
     <td>&nbsp&nbsp&nbsp $Start</td>
     <td>&nbsp&nbsp&nbsp $End</td>
     <td>&nbsp&nbsp&nbsp $lunch</td>
     <td>&nbsp&nbsp&nbsp $Clock</td>
     <td>&nbsp&nbsp&nbsp $status</td>
     <td>&nbsp&nbsp&nbsp $util </td>
     <td>&nbsp&nbsp&nbsp $hours</td>
     <td>&nbsp&nbsp</td>
     <td><input type='text' size='2' value = $reg name='payroll_data_reg_${id}' id='${r}c1' tabindex='$t1'/></td>
     <td><input type='text' size='2' value = $ot1 name='payroll_data_ot1_${id}' id='${r}c2' tabindex='$t2'/></td>
     <td><input type='text' size='2' value = $ot2 name='payroll_data_ot2_${id}' id='${r}c3' tabindex='$t3'/></td>
     <td><input type='text' size='2' value = $vac name='payroll_data_vac_${id}' id='${r}c4' tabindex='$t4'/></td>
     <td><input type='text' size='2' value = $sta name='payroll_data_sta_${id}' id='${r}c5' tabindex='$t5'/></td>
     <td><input type='text' size='2' value = $sic name='payroll_data_sic_${id}' id='${r}c6' tabindex='$t6'/></td>
     <td><input type='text' size='2' value = $brv name='payroll_data_brv_${id}' id='${r}c7' tabindex='$t7'/></td>
     <td><input type='text' size='2' value = $abs name='payroll_data_abs_${id}' id='${r}c8' tabindex='$t8'/></td>
     <td><input type='text' size='2' value = $loa name='payroll_data_loa_${id}' id='${r}c9' tabindex='$t9'/></td>
     <td><b id="warn">&nbsp&nbsp&nbsp $comm</b></td>
   </tr>
ENDHEADER
$r ++;
$tab++;
 }
 print <<ENDHEADER;
</table>
<br/><br/>
<input type='submit' value='Update Payroll Hours'>
<br/><br/>
<a href="${cgi_ref}trak_00_sup_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
<br/><br/>
<b id="info">$msg</b>
</form>
</body>
</html>
ENDHEADER
}



sub trak_03_sup_activity_selection_with_message
{
my $loc = $_[0];
my $date = $_[1];
my $num =  $_[2];
my $name =  $_[3];
my $msg =  $_[4];
# print "$loc, $date, $num, $name, $msg<br/>";
my $week_ref = get_payroll_review_weeks;
my @weeks = @$week_ref;
# print "HEY $weeks[0][0] - $weeks[0][1] : $weeks[1][0] - $weeks[1][1] : $weeks[2][0] - $weeks[2][1] <br/>";
# my $msg = & get_message ($num);
# my @dis = & get_disabled_list ($num);
# my @col = & get_dimmed_list ($num);
my @dis = ("", "", "", "", "", "", "", "disabled=\"disabled\"","disabled=\"disabled\"");
my @col = ("dis","en","en","en","en","en","en","dis","dis");

print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_sup_activity_select</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>
Activity:
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_sup_activity.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empLoc value='$loc'/>
<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input $dis[1]type="radio" name="activity" value="1" checked="checked"/>&nbsp<$col[1]> Confirm Daily Payroll</$col[1]><br/>
<input $dis[2]type="radio" name="activity" value="$weeks[0][0]"/>&nbsp<$col[2]> Review Payroll - $weeks[0][1] </$col[2]><br/>
<input $dis[3] type="radio" name="activity" value="$weeks[1][0]"/>&nbsp<$col[3]> Review Payroll - $weeks[1][1] </$col[3]><br/>
<input $dis[4] type="radio" name="activity" value="$weeks[2][0]"/>&nbsp<$col[4]> Review Payroll - $weeks[2][1]</$col[4]><br/>
<input $dis[5] type="radio" name="activity" value="5"/>&nbsp<$col[5]> Add New Temp Employee</$col[5]><br/>
<input $dis[6] type="radio" name="activity" value="6"/>&nbsp<$col[6]> Who is logged in?</$col[6]><br/>
<input $dis[6] disabled="disabled" type="radio" name="activity" value="7"/>&nbsp<$col[7]> TBA</$col[7]><br/>
<br/>
<input type='submit' value='Proceed'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl">Return to Log-In</a>
<br/><br/>
<b id="info">$msg</b>
</form>
</body>
</html>

ENDHEADER
}





